package com.neusoft.nepm.service.impl;

import com.neusoft.nepm.dto.NotificationRequestDto;
import com.neusoft.nepm.service.INotificationService;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;

@Service
public class NotificationServiceImpl implements INotificationService {

    private static final Logger logger = LoggerFactory.getLogger(NotificationServiceImpl.class);

    @Override
    public boolean sendNotification(NotificationRequestDto request) {
        logger.info("【通知标题】{}", request.getTitle());
        logger.info("【通知内容】{}", request.getContent());
        logger.info("【接收对象】{}", request.getTarget());
        logger.info("【发送渠道】{}", request.getChannels());

        if ("role".equals(request.getTarget())) {
            logger.info("【特定角色】{}", request.getRole());
            // 根据角色名称查询用户并发送通知
            sendNotificationToRole(request);
        } else {
            // 发送给全体用户
            sendNotificationToAll(request);
        }

        return true;
    }

    private void sendNotificationToRole(NotificationRequestDto request) {
        // 查询角色对应的用户并发送通知
        // 假设有一个方法 getRoleUsers(String role) 返回用户列表
        List<String> users = getRoleUsers(request.getRole());
        for (String user : users) {
            logger.info("发送通知给用户：{}", user);
            // 发送通知逻辑
        }
    }

    private void sendNotificationToAll(NotificationRequestDto request) {
        // 发送给全体用户的逻辑
        logger.info("发送通知给全体用户");
    }

    private List<String> getRoleUsers(String role) {
        // 查询数据库获取角色对应的用户列表
        // 这里是示例代码，实际实现需要根据你的数据库结构来编写
        return Arrays.asList("user1", "user2", "user3"); // 示例数据
    }
}